package cn.com.captureNetPacket.util;

import java.io.UnsupportedEncodingException;
import java.util.Map;

import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;

import cn.com.captureNetPacket.CaptureNetPacketService;
import cn.com.captureNetPacket.StartApplication;
import cn.com.captureNetPacket.model.DeviceStatus;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class RedisMessageListener implements MessageListener{

	@Override
	public void onMessage(Message message, byte[] pattern) {
		 
		String patternString="";
		 try {
			 if(pattern!=null)
			  patternString=new String(pattern,"UTF-8");
			 
		} catch (UnsupportedEncodingException e) {
			log.error("RedisLeaderMessageListener ",e);
		} 
		 DeviceStatus deviceStatus=null;
		 try {
		 log.info("收到RedisLeaderMessageListener 订阅消息：{} patternString: {}", message,patternString);
		 Map <String,Object> mesSta=JsonPathUtil.getValue(message.toString(), "$",Map.class);
		String messType= mesSta.get("messType").toString();
		Map <String,Object> payload=(Map<String, Object>) mesSta.get("payload");
		 if(messType.equals("config")) {
			 if(payload.get("command").toString().equals("reloadDeviceInfo")) {
				 //重新获取配置信息
				 CaptureNetPacketService.getCaptureNetPacketService().initDeviceInfo();
			 }
			 
			 
			 if(payload.get("command").toString().equals("reloadAppData")) {
				 //重新获取配置信息
				 CaptureNetPacketService.getCaptureNetPacketService().restartinit();
			 }
		 } 
		 
		  
		 
		 }catch (Exception e) {
			 log.error("RedisLeaderMessageListener ",e);
		}
	}

}
